//
//  KCPredicateUtility.m
//  Jade
//
//  Created by king on 2016/12/27.
//  Copyright © 2016年 KC. All rights reserved.
//

#import "AppDelegate.h"
#import "KCStartVC.h"
#import "KCTabBarVC.h"
#import "NSDate+KCAdd.h"

BOOL mobilePredicate(NSString *mobileNum) {
    
    NSString *MOBILE = @"^1(3[0-9]|4[57]|5[0-35-9]|7[0135678]|8[0-9])\\d{8}$";
    NSPredicate *regextestmobile = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", MOBILE];
    return [regextestmobile evaluateWithObject:mobileNum];

}

UIImage *imageWithWaterMask(UIImage *origImage) {
    
    UIImage *logo = [UIImage imageNamed:@"JadeLOGO"];
    UIGraphicsBeginImageContext(origImage.size);
    [origImage drawInRect:CGRectMake(0, 0, origImage.size.width, origImage.size.height)];
    [logo drawInRect:CGRectMake(10, 10, 40, 40)];
    UIImage *resultImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return resultImage;
    
}

void restartAndDeleteAllData() {
    
    KCStartVC *startVC = [[KCStartVC alloc]init];
    KCNavigationVC *navi = [[KCNavigationVC alloc]initWithRootViewController:startVC];
    [ShareApplicationDelegate.window.rootViewController presentViewController:navi animated:YES completion:nil];
    
}

void checkNet() {
    
    AFNetworkReachabilityManager *mgr = [AFNetworkReachabilityManager sharedManager];
    
    [mgr startMonitoring];
    
    [mgr setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
        
        // 当网络状态改变了, 就会调用这个block
        switch (status) {
            case AFNetworkReachabilityStatusUnknown: // 未知网络
                KCLog(@"未知网络");
                ShareApplicationDelegate.netState = 0;
                break;
                
            case AFNetworkReachabilityStatusNotReachable: // 没有网络(断网)
                KCLog(@"没有网络(断网)");
                ShareApplicationDelegate.netState = -1;
                break;
                
            case AFNetworkReachabilityStatusReachableViaWWAN: // 手机自带网络
                KCLog(@"手机自带网络");
                ShareApplicationDelegate.netState = 1;
                break;
                
            case AFNetworkReachabilityStatusReachableViaWiFi: // WIFI
                KCLog(@"WIFI");
                ShareApplicationDelegate.netState = 2;
                break;
            default:
                break;
        }
        
    }];
    
    
}


void refreshSystemFont() {
    
    if ([[DEFAULTS objectForKey:SYSTEM_FONT_CHANGE] isEqualToString:@"small"]) {
        FONT_NUM = 0;
    }else if ([[DEFAULTS objectForKey:SYSTEM_FONT_CHANGE] isEqualToString:@"center"]) {
        FONT_NUM = 2;
    }else if ([[DEFAULTS objectForKey:SYSTEM_FONT_CHANGE] isEqualToString:@"big"]) {
        FONT_NUM = 4;
    }else {
        FONT_NUM = 2;
    }
    
}


NSString *getMMSSFromSS(NSInteger seconds) {
        
    //format of day
    NSString *str_day = [NSString stringWithFormat:@"%02ld",seconds/(3600*24)];
    //format of hour
    NSString *str_hour = [NSString stringWithFormat:@"%02ld",(seconds%(3600*24))/3600];
    //format of minute
    NSString *str_minute = [NSString stringWithFormat:@"%02ld",(seconds%3600)/60];
    //format of second
    NSString *str_second = [NSString stringWithFormat:@"%02ld",seconds%60];
    //format of time
    NSString *format_time = [NSString stringWithFormat:@"%@:%@:%@",str_hour,str_minute,str_second];
    
    if (seconds/(3600*24) >= 1) {
        format_time = [NSString stringWithFormat:@"%@天%@时%@分",str_day,str_hour,str_minute];
    }
    
    return format_time;
    
}


//获取图片某一点的颜色
UIColor *colorAtPixel(CGPoint point) {
    
    UIImage *image = [UIImage imageNamed:@"lookup"];
    
    if (!CGRectContainsPoint(CGRectMake(0.0f, 0.0f, image.size.width, image.size.height), point)) {
        return nil;
    }
    
    NSInteger pointX = trunc(point.x);
    NSInteger pointY = trunc(point.y);
    CGImageRef cgImage = image.CGImage;
    NSUInteger width = image.size.width;
    NSUInteger height = image.size.height;
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    int bytesPerPixel = 4;
    int bytesPerRow = bytesPerPixel * 1;
    NSUInteger bitsPerComponent = 8;
    unsigned char pixelData[4] = { 0, 0, 0, 0 };
    CGContextRef context = CGBitmapContextCreate(pixelData,
                                                 1,
                                                 1,
                                                 bitsPerComponent,
                                                 bytesPerRow,
                                                 colorSpace,
                                                 kCGImageAlphaPremultipliedLast |     kCGBitmapByteOrder32Big);
    CGColorSpaceRelease(colorSpace);
    CGContextSetBlendMode(context, kCGBlendModeCopy);
    
    CGContextTranslateCTM(context, -pointX, pointY-(CGFloat)height);
    CGContextDrawImage(context, CGRectMake(0.0f, 0.0f, (CGFloat)width, (CGFloat)height), cgImage);
    CGContextRelease(context);
    
    CGFloat red   = (CGFloat)pixelData[0] / 255.0f;
    CGFloat green = (CGFloat)pixelData[1] / 255.0f;
    CGFloat blue  = (CGFloat)pixelData[2] / 255.0f;
    CGFloat alpha = (CGFloat)pixelData[3] / 255.0f;
    
    return [UIColor colorWithRed:red green:green blue:blue alpha:alpha];
}

//完整的图片URL
BOOL fullImgUrl(NSString *str) {

    return [str hasPrefix:@"http"];
    
}

NSURL *getImageUrl(NSString *imgStr) {
    if (imgStr) {
        if (fullImgUrl(imgStr)) {
            return [NSURL URLWithString:[NSString stringWithFormat:@"%@-showImage",imgStr]];
        }
        return [NSURL URLWithString:[NSString stringWithFormat:@"https://m2.icangyu.com%@",imgStr]];
    }
    return nil;
}
